//
//  FeedbackViewModel.swift
//  User
//
//  Created by ET|冰琳 on 2017/1/9.
//  Copyright © 2017年 IB. All rights reserved.
//

import UIKit
import ReactiveSwift

open class FeedbackViewModel<T: FeedbackService>: NSObject {
    
    open var feedback = MutableProperty<String?>(nil)
    open var feedbackValide = MutableProperty(false)
    open var feedbackValidator: Validator?
    
    private var service: T
    
    open var commit: Action<(), T.FeedbackResult, NSError>!
    
    /// - Parameter service: FeeedbackService
    public init(service: T) {
        self.service = service
        super.init()
        self.bind()
    }
    
    private func bind() {
        
        feedbackValide <~ feedback.signal.map({
            let validator = self.feedbackValidator ?? DefaultValidator()
            return validator.validate(txt: $0)
        })
        
        commit = Action {
            return self.service.feedback(txt: self.feedback.value)
        }
    }
    
}
